package com.jc.utils;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class CaptchaUtils {
    private static final String base = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefhijklmnpqrstuvwxyz";
    private static final int length = base.length();
    private static final String[] FONT_FACE_ARRAY = { "Arial", "Arial Black", "Arial Italic", "Courier New Bold Italic",
                                                      "Courier New Italic", "Franklin Gothic Medium", "Franklin Gothic Medium Italic" };
    private static final int[] FONT_SIZE_ARRAY = {28,30,32,34,36};
    private static final int[] LINE_LOOPS_ARRAY = {3,4,5};

    public static BufferedImage createCaptchaImage(int width, int height,
                                                   String randomCode) {
        final BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        final Graphics2D g = image.createGraphics();
        final Random random = new Random();
        g.setColor(new Color(230, 230, 250)); // 设置背景色
        g.fillRect(0, 0, width, height); // 填充背景
        g.setColor(Color.BLACK);
        // 绘制边缘
        g.drawRect(-1, -1, width + 1, height + 1);
        g.setColor(Color.GRAY);
        // 设置字体,随机选择字体,暂设置8种字体
        Font font = new Font(FONT_FACE_ARRAY[random.nextInt(7)], Font.PLAIN, FONT_SIZE_ARRAY[random.nextInt(5)]);
        // g.setStroke(new BasicStroke((float)(Math.random()),
        // BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
        g.setFont(font);
        // 定义字体颜色范围
        int red = random.nextInt(160), green = random.nextInt(50), blue = random
                .nextInt(50);
        for(int j$ = 0; j$ < LINE_LOOPS_ARRAY[random.nextInt(3)]; j$++){
            // 定义一个无干扰线区间和一个起始位置
            int nor = random.nextInt(50), rsta = random.nextInt(131);
            // 绘制干扰正弦曲线 M:曲线平折度, D:Y轴常量 V:X轴焦距
            int M = random.nextInt(15) + 5, D = random.nextInt(23) + 15, V = random
                    .nextInt(5) + 1;
            double x = 0.0;
            double y = M * Math.sin(Math.toRadians(V * x)) + D;
            double px, py;
            for (int i = 0; i < width; i++) {
                px = x + 1;
                py = M * Math.sin(Math.toRadians(V * px)) + D;
                if (rsta < i && i < (rsta + nor))
                    g.setColor(new Color(230, 230, 250));
                else
                    g.setColor(new Color(red, green, blue));
                // 随机设置像素点宽带(线宽)
                g.setStroke(new BasicStroke((float) (Math.random() + 0.5f)));
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g.draw(new Line2D.Double(x, y, px, py));
                x = px;
                y = py;
            }
        }

        char[] codes = randomCode.toCharArray();
        for (int i = 0; i < codes.length; i++) {
            // 旋转图形
            int degree = (random.nextInt(20) - 10) % 360;
            double ang = degree * 0.0174532925; // 将角度转为弧度
            g.rotate(ang, width / 2, height / 2);
            g.setColor(new Color(red, green, blue));
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            int gr = random.nextInt(8);
            g.drawString(String.valueOf(codes[i]), 24 * i + 10 + gr, 38);
        }
        g.dispose();
        return image;
    }

    public static String createRandomCode(int size) {
        StringBuffer randomCode = new StringBuffer();
        for (int i = 0; i < size; i++) {
            Random random = new Random();
            int start = random.nextInt(length);
            String strRand = base.substring(start, start + 1);
            randomCode.append(strRand);
        }
        return randomCode.toString();
    }
    
    public static void main(String[] args){
    		String base = "23456789abcdefhijkmnpqrstuvwxyz";
    		String s20160506 ="2a8rwp,xmn452,kb8kk9,ztaqiu,fjryja,hddtvf,vcs94r,nrkj2v,8dptb3,i6nn5h,hb2axa,v9zsbj,i34dn8,c6vn2d,5vpjda,ssmcqv,iiief3,iepkq4,unedby,4pcmcz,rd9v9m,3s2793,kjshds,uvvuuw,5qycus,jxyxqj,xyrmmk,z6w3nd,kqi4na,2yq9s3,tpcrfh,rc4fxs,hiyr9y,rq47yq,zhvkpz,d4ir44,r9iyys,ax8xis,f7cw7u,3ssar7,bx5m8f,9h6pap,w6ptmu,xanhuk,nvc578,2tahb5,msdahe,fxpjx3,rihuif,m3vpuw,hi2edv,yi6djd,rbxcni,h6vykh,hftzdy,wvstvd,dfdkzk,dcd3a3,2nymbv,k24sze,d6h7q6,k9hpas,vax23r,5zcrr9,qqnqhm,b6z6pt,vdbjbu,8eszup,9swdaa,47urtt,c4ae5n,ri9px7,btzpb4,5cvp67,p6vvk7,uzfpir,t6yzrh,i6a2um,ax5zcc,ayadbi,e5utnw,9q747u,t6cdww,ju4thn,f2phpx,qbywfj,46qadb,f4eze2,y8tfc9,unhz9k,apdkf2,38uvn8,cphaht,fk643k,6s7c8b,8cuqxq,aaymst,d64z8z,y5ee3s,vvrshh,7qwp4u,ajk25c,aivee5,2vtqj4,8a7a4r,zv82ux,727aph,pbjc7s,bfwk3c,yktfjd,3dz9bq,a43dz7,7ckt25,fr9f94,trm66q,4yvmjd,p9xyzw,fzpmt5,v2a8zp,qrqjvv,mzhk67,kiabkb,ubwky8,b7xuhc,6qfh4x,phxr2a,d9vn6c,44yk5r,t39qzb,w73sw3,zhb7fa,6batf3,hicq6e,yfcnps,rpzq8p,wd7cpt,tz25qt,3bjx79,b7eqsj,6maaz2,rbjbdi,vwun6r,zd7e3b,tukq64,b55c7f,74bv9x,2pstvz,jhw3vf,kp3ptm,nazk8y,7x8nk8,dvy5xt,n3zc4d,q7nhzj,nnhqiw,dxy4fs,79bkc4,z3ss23,ci35jt,t4fjre,7je5hm,6yzekz,v2dmjd,pnesee,qj8bnh,9uad4u,cqv4da,y363rx,7swbk5,d6smbk,cjv4zp,dipkqu,n74h8n,aajwib,78a747,p3jr39,pu3rpe,5wjzfq,wzdsjb,dkjy8b,tf7jzv,fk5xa2,yj7qih,s274ik,iha84v,hey4k4,bcnjc3,24k84f,i5qwmi,vmrtid,nbtmsh,3hhr42,6jkse9,jcjfa8,ies77w,rw8u3y,hd3tsq,xc74kr,5p54cf,385eda,tdcjfd,ift3hz,zbvysv,ap5aav,d8zsx2,wwj64f,vb5nry,sbaak2,a2uurd,9cr4a8,b92v4t,dai3nk,wqkfns,8r4zaj,qa22vx,i3iizk,yj5rv8,2ywszq,c9frtx,jcv53f,4na6ca,kxexii,f28vpq,c9vztz,yasvji,c6t5wv,kirtzk,ihq53q,tymvkw,2hrsue,6a8k39,u7bchh,sa6tb4,vqrshn,fnnsua,22dzxh,sc4mvw,xb4ces,wx7dpk,thcycb,ynq8vu,ivqdps,jf22h8,jcsc7t,tvfmbj,tnms3n,8eevvs,a4zvaf,3kvem8,9q7zuv,ym3akx,7yk3vi,bphjnm,yjhnzq,5cxknt,y24bpy,y6zz67,nymcc7,rm2q2z,etufuk,45qaxd,ijaajq,5swvu5,785etx,iq5xa7,uuivxz,fd92xp,3sckzm,i338kw,9uqj9y,p39a87,y9qebr,w2hiqn,766w58,3ymq2y,nt7hse,6f9k4k,53i3dd,uk62zk,re7nfa,a46d9q,3hi4yv,e9khyx,qid8i8,vnc47h,f8ujb4,sq4hp7,w6zx8c,8fzpd2,yztw9t,8nsa34,ac2fr8,6bm5fr,7vhh9y,cbtj4y,9ip28w,cfnum3,vfxptz,8iande,qmvhzv,b9yyj5,xqktuy,wqtmh5,s8j3q9,zz5hra,6edxhh,7pfyyx,vi9e3j,d876tj,zcxt88,nyvb7z,mr2ktu,4ei3qa,a3dckq,mec235,i2duuw,zzifqw,vtyuqd,6pri9t,nr6hnx,p879z5,beu2cm,ufp5rn,xmyyur,z2wex2,7qvr75,3fmw4z,a2q6is,7c3tpz,e9y5u2,nfds44,d9mbu2,pyxca9,ka4iny,f8qh3q,2b6csz,fe95kw,3hnedz,bh2zc7,ud6t9d,pzv63x,76emii,cib2uf,2d3k5v,7dmnzh,ebsptr,vxvmvk,5423u8,qa7uje,uymxht,h69yh7,p3kwnh,h354w6,edw46q,4hhbw9,5a8vwj,tzphi2,bhwmth,c9pcvh,yr43kr,9cykac,ucz2t5,2faj9z,a9khp5,36nbt3,3xyfv5,zpdy6d,t5yd2r,mvam6c,fwxkd4,najssz,t72z4m,a8sjtq,5uqkre,4a8mq3,4bnsj3,2tene4,ykny6u,wirz2y,fq4jun,fcja9x,z547um,7z3wkw,3x54kd,d8ddrq,wtdepx,s2sqix,wyft7a,pwe43x,uw9mm9,vtzezc,jhfip6,pt99kh,s3s2ti,qw24pi,p5ty2t,qavvyy,44wsn6,kf6ine,kcwdis,4ws96f,nhqz44,wk6una,zeuyva,q5i5w4,5m323y,55dxbm,yedrjm,hib93k,zwa3up,es36jr,rabzbr,qctenh,qukzpf,qs3z4m,f6fvzc,hffye8,v6nef9,zcsmqv,td38sb,76h2rf,8dr3vi,m5bmsa,ka2ttw,jxu5dx,m6u73d,3e9mxn,42dvw5,yhzdjr,r739te,hhx4av,epkkx2,zhn7yu,v5tn7b,rwcbqt,eccury,s8ncdu,v8e4jm,zy3wix,h72r43,8vkax2,aijws9,2i82bu,ceyxhw,k2mfd5,niaica,qtejq8,zrh7uw,5udvqw,6bjhn5,cdx7v7,haf7k5,9jcpkc,9imm25,8e7dvn,qsxbc3,nkk7xx,fux8rq,j2bvcd,t7enjb,v3ny9i,uwnsmc,996xm4,5f5hr4,6ir3pr,ku6wq5,bdx3wu,zvh4rf,vbd3es,waf73y,rer5uv,242ayp,5qmmcb,7csmez,pkd8yv,xpieia,4qj99x,kwvh3m,7b7q4t,89ds4d,v9ftxa,eq4ihk,tbh23n,453rpw,kvrf6q,7fc8jp,5ap466,sfc3qj,w5mqqc,zxfdt7,436f98,bqcnef,u2i5qw,p4bkqu,dh6dcz,8mzu9m,r7bk79,4qa3x5,sasev2,k9mn4v,i9eha5,uj95qj,9d3xvv,uuirhc,kns2ff";
    		String s20160507 ="vuchax,9cakzy,6t3v73,t9v2fc,sn75df,v2pzmn,n8b7dd,pvpcbp,zzfnec,4tsjtt,7ika3d,yhmxen,c4n9h9,vkfj3x,wrp2aq,kec7cw,wzb4xn,pex57p,bfyehq,v48q5y,uznfpt,swuqse,yqweip,62y6w3,7f2q5r,i4q9p8,x7crt3,57e47k,2t22zp,frai5a,dfrbyn,djbjvs,yzt7fx,3z8f6j,ttx7hj,nhhqv4,6a6bbs,5re9n7,q573xi,qwah2p,sbdtzf,5qa5nk,dehqjr,83h2tu,6nv5bp,vaznfc,9sd3ew,wrw7zq,mqvqyq,5jcvsu,iqbiaq,qvuvjt,uzdk2b,5c56b3,p28fzh,i6k3t6,ay634h,cuy5h7,4z6fha,ssfhyz,fww4v4,8jfabf,bipsxj,ekvrq4,wq5uu7,b4t9vr,48dv6e,s48hvb,ayk8k7,52763h,bycues,d3zbmv,5dc76r,n7iwyc,d7ybuh,u97mh3,s3auhe,2d3cer,kexisr,xmzk7q,itwwyh,6p8z8d,nqtd5t,ck22pf,4sbzz2,bn5rqj,k2fyhh,q9cp75,pi32i8,q9zh5s,efvdud,5vqunr,z9n58f,ajzwvr,yqjx69,42apmq,e9fpxm,yhnw5t,fe7fxf,xpqmfd,6fpiid,n7dew4,5hydr4,fjrj6t,3aaxbu,89bjza,xy562w,55jmk6,5wjab9,ksakwu,eyc9pe,akweq8,zq7746,9cr46a,88ktmk,wtrep7,9ye388,zwq4cr,yh6t2q,ccz6ik,9hkcc7,fr9knq,94673x,ybsa9h,itd8vi,3myyk7,emuu4k,48ewty,5hvupd,rvmk5b,635nqf,uwucc5,zubv9s,ikxin8,efxs7e,a5fij5,wb6ux3,d4u4nn,ir5xvy,zir6dq,eciad9,a9n9mf,4c3j82,w7einj,kzft3b,wxbia8,39uxqy,abch7s,5msy7r,vhn2r2,8jm9bp,3bk2pj,7678bj,qwsv7x,x93zw8,dmfpct,xnxha8,h6mvnq,mxnqq6,6seh83,8wrnh8,xpeadt,bab2c8,kmv5dt,ecmrqj,pqhk22,7kapza,zcffmd,6acu3j,k72uk7,b9xfmu,t5i2qq,ruf5bk,eh634e,wzir9i,zzayai,raxby7,pwk3c5,tr7zqw,cr96mk,srmduw,8yjayc,pwqwxj,w7fzaj,w5kusy,x35ahj,dmjh95,sci9pt,mpy28y,pwbszh,rpjw7i,d6vs7f,zj4m8x,d9auyw,t3xnj7,c3nmkc,vbfbn2,abiv58,c4jx2q,329a5y,swwz88,mb2xs9,x59iwv,55r635,udyidb,t5zex5,pyjiyh,nnu2rh,x26hk3,5kt2u4,nyhcwu,cnyxpy,w393rx,t782bd,f5r3qz,u4we6j,ksrrma,wcdsrk,sbbd4t,2ri2hj,t55czx,dkwfnu,re8qyu,xeyt77,cc2rji,dmajsj,u58tj6,t7n95u,3jbdpb,ckvprc,rns4xv,vxvzt4,mzah9n,hu7nnm,pdshqd,c4ncy2,fd2xz2,a5v9mw,3kdphu,cznjeq,jt5fe9,b5kcm4,yjmtze,546c9e,wt9ut6,pq7rau,jpiktu,n395nn,kwmdbp,hwd25w,et59c7,jpr89f,cpfrdx,xrr85t,ubzhch,bp7aj8,qvz7jj,v9kzih,icms28,jjc7e5,993h87,fkh7sc,yheza2,u62cku,d5enth,p2q5ei,84cyja,4k8zyr,ph7ne5,5rk7h2,kprr9j,rj2hrd,5yaaiz,z8dmuc,v8bm7k,sjmcmv,9q3zzd,xdbtqf,49x4ra,nezauz,t964r7,hqh7s2,dwydp8,hh9hp5,364u6s,mxpn9b,yz22rv,7kyrp5,frkcvh,7c4s7t,v7nf2m,yt4kd7,mkbpae,i2tyme,mzfyqi,k8ss8a,hxai2m,tea5sa,ehmu6t,atiheq,8qs2jc,f3qv99,42ip4v,bikur3,383mr2,h66ayd,fjxz9j,5d6qjn,7myk8w,63jzrm,xda2m4,ck34kx,xcwwms,2hmfb5,hqnt4n,dhpiwf,nwbakx,y5x3fb,4rqe47,d4xc3j,u8k3q6,nm2ncj,irzryj,xx4s4j,t9sp93,tpf826,p4xkjd,7qfq4d,jmwtck,mvsdua,hx5pa6,azmdcc,3ajyzw,qfkjss,8vrb3h,vfwnkw,iuj3mu,hjtkcq,43pvim,nsbymu,4nsyrp,fdmfxe,8j5wba,eh6a3f,mpaup8,btix63,5mbrsr,xsr3h5,2wbm4u,mtvai8,3q9kdn,irha23,9bcpuq,bjvqf7,din2rz,mq7pei,xpu5mt,tmwayh,5tnfba,pjx65p,thbtjy,2stq8e,y4nikt,8jzw7b,hwpxyx,rymbjj,tsptcq,k4e6zc,35ery6,uwdphd,92euvr,q4cwkf,qxdbyb,jpsben,k8jzz4,2fbn7c,mistr8,amurti,kpje8w,t8xa2v,tih8xm,ws6p3f,e4xyqc,a4cuv8,hhkyav,ri4x9n,67ntej,i8q4xz,ket576,a6fct4,sxr632,f4em4d,fhiypc,39t8ku,dacf8y,mawrx8,myhpaw,3necmz,jpz63a,xkrwyc,bcb8pa,wv3cqq,x4a5ex,7ek8wx,4eab26,y2pjcx,kpc7ch,rynfat,p64un5,vehady,ryxx4m,nuzht4,9n492v,8m7i8b,tckqaj,fxtd2x,t6uvez,w5ruuh,357tn2,tbmfzq,ewna25,qhssbw,p4aefq,y3tca3,hs3978,489pas,87si8d,c5ynwh,c4em84,j28ajn,zkdkxb,7bnj48,myfcx3,9fcsib,j2rw7v,cr5djs,f5snrq,a6f7i6,rdapwz,bmtjbx,ak6kq3,s9a5xq,nq47yx,tz4c7p,cui37r,rhu294,z93cxx,f42za6,d44jmf,6e54c5,h9jami,h64k5u,4u5pin,ra8wip,uvsaxf,h8yq4d,9i4q3k,huukpd,9bp8mt,hp2wms,zn6zfd,767972,h9ctqh,bt2w94,hjkjei,cq2d4e,b8yu32,s2vzz7,344fdh,2ijt6j,nqpvd4,awpp6z,ft6tz9,yynyic,enwrm4,i4tsz6,p4h2xs,csaej9,xnaetj,sexk5q,8eruf6,r3976n,qbf3nr,5hp6f6,htuv5r,mvfyvr,fppmsa,ayy9su,p3tk9t,nhwrrb,wab4qp,fpencd,wwdjh4,v6u62z,jun2kf,ceus6a,sqv55p,7w8f9t,tub8nz,cifdc7,6m7u5d,uz2aze,sarm9n,kktu9n,4eqzrp"	;	
    		List oldList = new ArrayList<String>();
    		oldList.addAll(Arrays.asList(s20160506.split(",")));
    		oldList.addAll(Arrays.asList(s20160507.split(",")));
    		int length = base.length();
    		Map<String,Integer> result = new HashMap<String,Integer>();
    		for(int j = 0; j < 100; j++){
    			while(true){
    				StringBuffer randomCode = new StringBuffer();
                    for (int i = 0; i < 6; i++) {
                        Random random = new Random();
                        int start = random.nextInt(length);
                        String strRand = base.substring(start, start + 1);
                        randomCode.append(strRand);
                    }
    				String key = randomCode.toString();
    				if(!result.containsKey(key) && !oldList.contains(key)){
    					result.put(key, Integer.valueOf(0));
    					break;
    				}
    			} 			
    		}
    		StringBuffer sb = new StringBuffer("20160506:");
    		int c = 0;
    		for(Map.Entry<String,Integer> entry:result.entrySet()){
    			if(c == 50 ){
    				System.out.println(sb.toString());
    				sb = new StringBuffer("20160507:");
    			}
    			sb.append(entry.getKey()).append(",");
    			if(c == 99 ){
    				System.out.println(sb.toString());
    			}
    			c++;
    		}
    }
}